﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Markup;

namespace Exones.Blend.AssetsManager
{
	[MarkupExtensionReturnType(typeof(IValueConverter))]
	class NotConverter: MarkupExtension, IValueConverter
	{
		public override object ProvideValue(IServiceProvider serviceProvider)
		{
			return this;
		}

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return !((bool) value);
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return !((bool)value);
		}
	}

	class NotExtension : Binding
	{
		public NotExtension()
		{
		}

		public NotExtension(string path)
			: base(path)
		{
			Converter = new NotConverter();
		}
	}

	public class AndMultiConverter : IMultiValueConverter
	{
		public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
		{
			var res = true;
			foreach (var value in values)
			{
				var bl = (bool)value;

				res = res && bl;
				if (!res)
					break;
			}
			return res;
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}

	public class AndExtension : MultiBinding
	{
		public AndExtension(Binding binding1)
		{
			this.Bindings.Add(binding1);
			this.Converter = new AndMultiConverter();
		}

		public AndExtension(Binding binding1, Binding binding2)
		{
			this.Bindings.Add(binding1);
			this.Bindings.Add(binding2);
			this.Converter = new AndMultiConverter();
		}

		public AndExtension(Binding binding1, Binding binding2, Binding binding3)
		{
			this.Bindings.Add(binding1);
			this.Bindings.Add(binding2);
			this.Bindings.Add(binding3);
			this.Converter = new AndMultiConverter();
		}
	}
}
